   package com.javalong.affairs.base.viewpagerfragment;

import java.util.ArrayList;
import java.util.List;

import com.javalong.affairs.base.utils.ListUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by 鏉ㄨ锲? on 2015/8/29 0029 viewpager+fragment 镄? 澶撮儴鎺т欢锛岀敤鏉ユ爣璇嗗綋鍓崭綅缃?
 */
public class ViewPagerStrip extends LinearLayout {
	// 鏂囨湰澶栬竟璺濓紝鍙湁宸﹀榻愮殑 镓嶉渶瑕佺敤鍒?
	private final static int TITLE_LEFT_MARGIN = 20;
	private final static int TITLE_RIGHT_MARGIN = 20;
	private final static int TITLE_TOP_MARGIN = 10;
	private final static int TITLE_BOTTOM_MARGIN = 10;
	// 搴曢儴婊氩姩鏉?
	private ViewPagerTitleBar vp_bar;
	// 鍙粦锷ㄦ爣棰?
	private HorizontalScrollView hs_title;
	// 涓嶅彲婊戝姩镙囬
	private LinearLayout ll_title;
	// 鐢变簬链?2涓笉鍚岀殑鍙兘锛岀敤vg鏉ヤ唬镟县s_title鎴杔l_title
	private ViewGroup vg;
	// 镙囬鏁扮粍
	private String[] titleTexts;
	// 瀵归绨绫诲瀷
	private ALIGN_TYPE alignType;
	// 瀛桦偍镙囬 textview锛屾粦锷ㄦ椂 璁＄畻锅忕Щ閲?
	private List<TextView> textViewList = new ArrayList<TextView>();
	// 褰揿墠阃変腑镄勪綅缃?
	private int currIndex = 0;

	public enum ALIGN_TYPE {
		// 宸﹀榻?
		ALIGN_LEFT,
		// 灞呬腑锛岀瓑鍒?
		MATCH_AVERAGE
	}

	public ViewPagerStrip(Context context, String[] titleTexts,
			ALIGN_TYPE alignType) {
		super(context);
		this.titleTexts = titleTexts;
		this.alignType = alignType;
		init();
	}

	public ViewPagerStrip(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		if (alignType == null) {
			return;
		}
		this.setOrientation(LinearLayout.VERTICAL);
		vp_bar = new ViewPagerTitleBar(getContext());
		LinearLayout.LayoutParams ll_param = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		switch (alignType) {
		case ALIGN_LEFT:
			vg = hs_title = new HorizontalScrollView(getContext());
			this.addView(hs_title, ll_param);
			break;
		case MATCH_AVERAGE:
			vg = ll_title = new LinearLayout(getContext());
			this.addView(ll_title, ll_param);
			break;
		}
		this.addView(vp_bar, ll_param);
		initTitle();
		initBottomLine();
	}

	// 鍒濆鍖栧簳閮ㄦ粴锷ㄦ浔
	private void initBottomLine() {
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// resignBottomLine();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		resignBottomLine();
	}

	// 閲嶆柊璁＄畻 搴曢儴line镄? 浣岖疆
	private void resignBottomLine() {
		TextView textView = ListUtils.getSafeItem(textViewList, currIndex);
		if (textView == null) {
			return;
		}
		float textSize = textView.getText().length() * textView.getTextSize();
		float textOffset = (textView.getWidth() - textSize) / 2;
		vp_bar.setLocation((int) textSize,
				(int) (textView.getLeft() + textOffset));
		vp_bar.invalidate();
	}

	// 鍒濆鍖栨爣棰?
	private void initTitle() {
		LinearLayout.LayoutParams ll_param = null;
		switch (alignType) {
		case ALIGN_LEFT:
			ll_param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			ll_param.setMargins(TITLE_LEFT_MARGIN, TITLE_TOP_MARGIN,
					TITLE_RIGHT_MARGIN, TITLE_BOTTOM_MARGIN);
			break;
		case MATCH_AVERAGE:
			ll_param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT, 1);
			break;
		}
		if (ll_param == null) {
			return;
		}
		addTitleByLayoutParam(ll_param);
	}

	/**
	 * @param ll_param
	 *            镙规嵁aligntype 涓嶅悓锛? 杩涜涓嶅悓澶勭悊
	 */
	private void addTitleByLayoutParam(LayoutParams ll_param) {
		if (titleTexts == null) {
			return;
		}
		switch (alignType) {
		case ALIGN_LEFT:
			LinearLayout linearLayout = new LinearLayout(getContext());
			for (String title : titleTexts) {
				TextView textView = new TextView(getContext());
				textViewList.add(textView);
				textView.setText(title);
				linearLayout.addView(textView, ll_param);
			}
			vg.addView(linearLayout);
			break;
		case MATCH_AVERAGE:
			for (String title : titleTexts) {
				TextView textView = new TextView(getContext());
				textViewList.add(textView);
				textView.setText(title);
				textView.setGravity(Gravity.CENTER);
				vg.addView(textView, ll_param);
			}
			break;
		}
	}
}
